package com.mall.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.bean.User;
import com.mall.mapper.UserMapper;
import com.mall.service.UserService;
import com.mall.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by xiaochao on 2017/7/23.
 *
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    public PageInfo<User> getUserList(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<User> list = userMapper.getUserList();
        PageInfo<User> page = new PageInfo<User>(list);
        return page;
    }

    public List<User> find() {
        return userMapper.getUserList();
    }

    public User selectByUserNamePassword(String username, String password) {
        return userMapper.selectByUserNamePassword(username,password);
    }

    public User selectByName(String username) {
        return userMapper.selectByName(username);
    }

    public User selectByUserId(String userNo) {
        return userMapper.selectByUserId(userNo);
    }

    public int insertUser(User user) {
        user.setId(SecurityUtil.generateUUID());
        user.setPassword(SecurityUtil.getEncryotString(user.getPassword(),user.getUsername()));//密码加密
        user.setCreated(new Date());
        return userMapper.insertUser(user);
    }

    public int insertUserByUser(User user) {
        return userMapper.insertUserByUser(user);
    }

    public int deleteById(String id) {
        return userMapper.deleteById(id);
    }

    public int updateByUserId(User user) {
        user.setPassword(SecurityUtil.getEncryotString(user.getPassword(),user.getUsername()));//密码加密
        user.setUpdated(new Date());
        return userMapper.updateByUserId(user);
    }
}
